package cn.u313.listener;

import cn.u313.comm.conststring.粽子;
import cn.u313.comm.utils.Util;
import cn.u313.service.DragonBoatFestivalService;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.filter.MatchType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.ParseException;

import static cn.u313.comm.conststring.粽子.zzRo1;
import static cn.u313.comm.utils.Util.getHour;
import static cn.u313.comm.utils.Util.getNewYear;

/**
 * 端午节活动
 */
//@Component
public class DragonBoatFestivalListen extends BaseListen{
    @Autowired
    DragonBoatFestivalService dragonBoatFestivalService;
    @Filter(value = "活动",matchType = MatchType.STARTS_WITH)
    @Filter(value = "端午",matchType = MatchType.STARTS_WITH)
    @Filter(value = "菜单",matchType = MatchType.STARTS_WITH)
    @OnGroup
    public void menu(GroupMsg groupMsg, MsgSender msgSender){
        String aa = "端午活动菜单如下：\n" +
                "[CAT:face,id=66]包粽子\n" +
                "[CAT:face,id=66]抽粽子\n" +
                "[CAT:face,id=66]抢粽子\n" +
                "[CAT:face,id=66]吃粽子\n" +
                "[CAT:face,id=66]送粽子\n" +
                "[CAT:face,id=66]万粽归心\n" +
                "[CAT:face,id=66]粽子查询\n" +
                "[CAT:face,id=66]给小嘤送粽子有惊喜o\n" +
                "[CAT:face,id=66]签到，抢劫，吃鱼都有概率获得粽子哦";
        sendMsg(msgSender,groupMsg,aa);
    }
    @OnGroup
    @Filter(value = "粽子",matchType = MatchType.STARTS_WITH)
    @Filter(value = "查询粽子",matchType = MatchType.STARTS_WITH)
    public void zongz(GroupMsg groupMsg, MsgSender msgSender){
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"无权操作！");
            return;
        }
        String msg= "有下列粽子可供选择：\n" +
                粽子.包粽子()+
                "\n回复：包白米粽【咸】\n" +
                "或：包白米粽【甜】";
        sendMsg(msgSender,groupMsg,msg);
    }
    @Async
    @OnGroup
    @Filter(value = "包.*",matchType = MatchType.REGEX_FIND)
    @Filter(value = "抽.*粽子",matchType = MatchType.REGEX_FIND)
    public void 包粽子(GroupMsg groupMsg, MsgSender msgSender) {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }
        String msg = groupMsg.getMsg();
        boolean is咸 = false;

        if(msg.contains("咸")){
            is咸 = true;
        }

        String name = (msg.contains("包"))?msg.substring(msg.lastIndexOf("包")+1,3):null;
        name = name==null||name.contains("粽")?name:name+"粽";
        System.out.println(name);
        if(name==null||name.length()<1||name.equals("包粽子")||name.equals("粽子")){
            is咸 = 粽子.randomtx();
            name = 粽子.random();
        }
        /**
         * 判断是否在这个范围
         */
        if(!粽子.haszz(name)||name.length()!=3){

            msg= "只有下列粽子可供选择：\n" +
                    粽子.包粽子()+
                    "\n回复：包白米粽【咸】\n" +
                    "或：包白米粽【甜】";
            sendMsg(msgSender,groupMsg,msg);
            return;
        }

        msg = dragonBoatFestivalService.executeZongZi(getQQ(groupMsg),name,is咸,getGroup(groupMsg));
        sendMsg(msgSender,groupMsg,msg);
    }
    @Async
    @OnGroup
    @Filter(value = "赠送粽子",matchType = MatchType.STARTS_WITH)
    @Filter(value = "送粽子",matchType = MatchType.STARTS_WITH)
    @Filter(value = "转赠粽子",matchType = MatchType.STARTS_WITH)
    public void 送粽子(GroupMsg groupMsg, MsgSender msgSender) {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }

        String msg = groupMsg.getMsg();
        if (msg.length()<5) {
//            菜单

            msg = dragonBoatFestivalService.showZongZiList(getQQ(groupMsg),getGroup(groupMsg),false);
        }else {
            String qq1 = getAt(groupMsg);
            if (qq1 == null) {
                msg = "你想把粽子送给谁呀\n艾特那个ta吧";
            }else
                msg = dragonBoatFestivalService.zs(getQQ(groupMsg),qq1,getGroup(groupMsg),getCode(groupMsg));

        }


        sendMsg(msgSender,groupMsg,msg);
    }


    @Async
    @OnGroup
    @Filter(value = "他的粽子",matchType = MatchType.STARTS_WITH)
    @Filter(value = "我的",matchType = MatchType.STARTS_WITH)
    public void 我的粽子(GroupMsg groupMsg, MsgSender msgSender) {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }
        String msg = groupMsg.getMsg();
        if (groupMsg.getMsg().length()<5) {
//            菜单

            msg = dragonBoatFestivalService.showZongZiList(getQQ(groupMsg),getGroup(groupMsg),true);
        }else{
//            菜单

            msg = dragonBoatFestivalService.showZongZiList(getQQ1(groupMsg),getGroup(groupMsg),true);
        }

        sendMsg(msgSender,groupMsg,msg);
    }
    @Async
    @OnGroup
    @Filter(value = "吃粽子",matchType = MatchType.STARTS_WITH)
    public void 吃粽子(GroupMsg groupMsg, MsgSender msgSender) {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }
        String msg = groupMsg.getMsg();
        if (groupMsg.getMsg().length()<4) {
//            菜单

            msg = dragonBoatFestivalService.showZongZiList(getQQ(groupMsg),getGroup(groupMsg),true)+"\n回复：吃粽子【编号】";
        }else{
//            菜单

            msg = dragonBoatFestivalService.eat(getQQ(groupMsg),getGroup(groupMsg),getCode(groupMsg))+"\n回复：吃粽子【编号】";
        }

        sendMsg(msgSender,groupMsg,msg);
    }

    @Async
    @OnGroup
    @Filter(value = "抢粽子.*",matchType = MatchType.REGEX_FIND)
    public void 抢粽子(GroupMsg groupMsg, MsgSender msgSender) {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }
        String msg = groupMsg.getMsg();
        if (groupMsg.getMsg().length()<4) {
//            菜单

            msg ="没有选择需要抢的对象哦！";
        }else{
//            菜单

            msg = dragonBoatFestivalService.robbery(getQQ(groupMsg),getGroup(groupMsg),getQQ1(groupMsg));
        }

        sendMsg(msgSender,groupMsg,msg);
    }
    @Async
    @OnGroup
    @Filter(value = "万粽归心.*",matchType = MatchType.REGEX_FIND)
    public void drawAprize(GroupMsg groupMsg, MsgSender msgSender) throws ParseException {
        if (notManager(groupMsg)){

            sendMsg(msgSender,groupMsg,"还未到活动时间哦！");
            return;
        }

        if (getHour()<20){

            sendMsg(msgSender,groupMsg,"端午三天，每晚8点开好运粽哦\n最高666悦币和8.8红包\n快去攒粽子吧！！");
            return;
        }

        if (zzRo1 == null) {
            sendMsg(msgSender,groupMsg,"好运粽未配置！");
            return;
        }

        String msg = dragonBoatFestivalService.rowzz(
                getQQ(groupMsg),getGroup(groupMsg));

        sendMsg(msgSender,groupMsg,msg);
    }
    @Async
    @OnGroup
    @Filter(value = "猫猫.*",matchType = MatchType.REGEX_FIND)
    public void cat(GroupMsg groupMsg, MsgSender msgSender) {


        sendMsg(msgSender,groupMsg,"[CAT:image,imageType=JPG,isEmoji=false,url=http://edgecats.net/?"+Math.random()+"]",false);
    }

}
